#pragma once

#include "MICEXTableField.h"
#include "MICEXTableKey.h"

#include "MTESrl.h"
#include <memory>
#include <string>
#include <map>

typedef std::vector<int32> KEYFIELDSVECTOR;
typedef std::vector<LPBYTE> TABLEDATAVECTOR;

class MICEXTable
{
public:

	enum TableFlags
	{
		ClearOnUpdate = 0x2
	};


	MICEXTable(void);
	virtual ~MICEXTable(void);


	void UpdateTableRow(int nFields, BYTE* pFieldBuf, int32 nDataLen, BYTE* pDataBuf);
	void ClearTableData();

	int32 TableHandle;
    std::string TableName;
	std::string TableDescr;

	int32 TableRef;

	int32 Attributes;

	int32 OutputFieldsCount;
	MICEXTableField* OutputFields;

	int32 InputFieldsCount;
	MICEXTableField* InputFields;

	int32 RowDataSize;

	std::string TableParams;
	int32 CompleteFlag;

	KEYFIELDSVECTOR KeyFields;
	int32 KeySize;

	TABLEDATAVECTOR TableData;
	TABLEKEYMAPTOPOSITION	TableKeys;

private:
	inline void SetFieldValue(int32 nFieldNum, LPBYTE pRowBuf, BYTE* pFieldBuf)
	{
		memcpy(pRowBuf + OutputFields[nFieldNum].FieldOffset, pFieldBuf, OutputFields[nFieldNum].FieldSize);
	}

};

typedef std::tr1::shared_ptr<MICEXTable> MICEXTablePtr;